package com.samphanie.auiu.biz.goods

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.samphanie.auiu.R
import com.samphanie.auiu.common.ui.view.InputItemLayout
import com.samphanie.auiu.databinding.LayoutGoodsDetailItemAttrBinding
import com.samphanie.auiu.model.GoodsDetailModel
import com.samphanie.auiu.ui.item.AuiuDataItem
import com.samphanie.auiu.ui.item.AuiuViewHolder

/**
 * 商品描述模块
 *
 * @author ZSY
 */
class GoodsAttrItem(val model: GoodsDetailModel): AuiuDataItem<GoodsDetailModel, AuiuViewHolder>() {

    private lateinit var binding: LayoutGoodsDetailItemAttrBinding

    override fun onBindData(holder: AuiuViewHolder, position: Int) {
        val context = holder.itemView.context?:return
        val goodsAttr = model.goodsAttr
        goodsAttr?.let {
            val iterator = it.iterator()
            var index = 0
            val attrContainer = binding.attrContainer
            attrContainer.visibility = View.VISIBLE
            while (iterator.hasNext()) {
                val attr = iterator.next()
                val entries = attr.entries
                val key = entries.first().key
                val value = entries.first().value

                val attrItemView: InputItemLayout = if (index < attrContainer.childCount) {
                    attrContainer.getChildAt(index) as InputItemLayout
                } else {
                    val inflateView:InputItemLayout = LayoutInflater.from(context)
                        .inflate(R.layout.layout_goods_detail_item_attr_item, attrContainer, false) as InputItemLayout
                    attrContainer.addView(inflateView)
                    inflateView
                }

                attrItemView.getEditText().hint = value
                attrItemView.getEditText().isEnabled = false
                attrItemView.getTitleView().text = key
                index++
            }
        }

        model.goodsDesc?.let {
            val attrDesc = binding.attrDesc
            attrDesc.visibility = View.VISIBLE
            attrDesc.text = it
        }

    }

    override fun getItemLayoutRes(): Int {
        return R.layout.layout_goods_detail_item_attr
    }

    override fun getItemView(parent: ViewGroup): View? {
        binding = LayoutGoodsDetailItemAttrBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return binding.root
    }
}